
package com.bangcommunity.bbframe.dynconfig;

import com.bangcommunity.bbframe.common.utils.io.FileCopyUtils;

import java.io.InputStreamReader;

/**
 * Created by tanghc on 17/1/23.
 */
public class InitQtucZk {

    // static DynConfigClient client = new DynConfigClient("10.254.208.189");
    static DynConfigClient client = null;
    static {
        client = new DynConfigClient();
        client.init();
    }

    public static void main(String[] args) throws Exception {
        initModule("basedata");
        initModule("ucm");
        initModule("oauth", true);
        initModule("uic");
        initModule("sso", true);
        initModule("bsso", true);
        initModule("ucw", true);
        initModule("psm", true);
        initModule("rest");
        initModule("facade");
    }

    public static void initModule(String module) throws Exception {
        initModule(module, false);
    }

    public static void initModule(String module, boolean useSession) throws Exception {
        initNoteByFile("/configs/qtuc/" + module + "/common/loglevel", "/loglevel.properties");
        initNoteByFile("/configs/qtuc/" + module + "/common/storageKs3Config", "/storageKs3Config.properties");
        initNoteByFile("/configs/qtuc/" + module + "/common/threadpool", "/threadpool.properties");
        initNoteByFile("/configs/qtuc/" + module + "/common/monitor", "/monitor.properties");
        if (module.indexOf("sso") != -1) {
            initNoteByFile("/configs/qtuc/" + module + "/common/dynconfig", "/dynconfig.properties.sso");
        } else {
            initNoteByFile("/configs/qtuc/" + module + "/common/dynconfig", "/dynconfig.properties");
        }
        initNoteByFile("/configs/qtuc/" + module + "/common/redisConfig", "/redisConfig.properties");
        initNoteByFile("/configs/qtuc/" + module + "/common/mqConfig", "/mqConfig.properties");
        initNoteByFile("/configs/qtuc/" + module + "/common/kafkaProducer", "/kafkaProducer.properties");
        initNoteByFile("/configs/qtuc/" + module + "/common/kafkaConsumer", "/kafkaConsumer.properties");
        initNoteByFile("/startconfigs/qtuc/" + module + "/config", "/config.properties", module, useSession);
    }

    public static void initNoteByFile(String note, String filename) throws Exception {
        initNoteByFile(note, filename, null, false);

    }

    public static void initNoteByFile(String note, String filename, String module, boolean useSession)
            throws Exception {

        InputStreamReader inputStreamReader = new InputStreamReader(
                InitQtucZk.class.getResource(filename).openStream());
        String s = FileCopyUtils.copyToString(inputStreamReader);
        if (useSession) {
            inputStreamReader = new InputStreamReader(InitQtucZk.class.getResource(filename + ".web").openStream());
            String s1 = FileCopyUtils.copyToString(inputStreamReader);
            s += "\n";
            s += s1;
        }
        if (null != module) {
            if (module.equals("psm")) {
                inputStreamReader = new InputStreamReader(
                        InitQtucZk.class.getResource(filename + ".web.usesso").openStream());
                String s1 = FileCopyUtils.copyToString(inputStreamReader);
                s += "\n";
                s += s1;
            } else if (module.equals("ucw") || module.equals("oauth")) {
                inputStreamReader = new InputStreamReader(
                        InitQtucZk.class.getResource(filename + ".web.usebsso").openStream());
                String s1 = FileCopyUtils.copyToString(inputStreamReader);
                s += "\n";
                s += s1;
            }
        }
        client.setConfig(note, s);
    }
}
